package com.sky.controller.admin;


import com.sky.controller.BaseController;
import com.sky.result.Result;
import com.sky.utils.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用模块")
public class CommonController extends BaseController {

    @Autowired
    private OssUtils ossUtils;

    // ignore_security_alert
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result upload(MultipartFile file, Long id) {
        log.info("上传文件信息 {} {}", file, id);
        // 原始文件名称
        String originalFilename = file.getOriginalFilename();
        StringBuilder builder = new StringBuilder();
        StringBuilder objectName = builder.append(System.currentTimeMillis()).append("_").append(originalFilename);
        try {
            String path = ossUtils.upload(file.getBytes(), objectName.toString());
            return Result.success("操作成功",path);
        } catch (IOException e) {
            log.error("文件上传失败 {}",e.toString());
        }
        return Result.error("文件上传失败");
    }
}
